/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.controller;

import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.editor.model.CharacterModel;
import offset.nodes.client.editor.model.DocumentContext;
import offset.nodes.client.editor.model.Range;

/**
 *
 * @author Walter Lütgenau
 */
public class CharacterActions {
    public static abstract class AbstractCharacterAction extends EditorActions.AbstractEditorAction {

        public AbstractCharacterAction(String name, Editor editor) {
            super(name, editor);
        }

        protected CharacterModel getModel(ActionEvent ae) {
            return new CharacterModel(getDocumentContext(ae));
        }

        protected CharacterModel getModel(Editor editor) {
            return new CharacterModel(new DocumentContext(editor.getBrowserPane()));
        }

    }
    
    public static class BoldAction extends AbstractCharacterAction {
        public void action(ActionEvent e) throws Exception {
            Range range = new Range(getEditor(e).getSelectionStart(), getEditor(e).getSelectionEnd());
            getModel(e).setBold(range);
            refreshCaret(e, range);
        }

        public BoldAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("insert.character.bold"), editor);
            setIcon(new ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_bold.png")));
            setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.CTRL_MASK));
        }
    }

    public static class ItalicAction extends AbstractCharacterAction {
        public void action(ActionEvent e) throws Exception {
            Range range = new Range(getEditor(e).getSelectionStart(), getEditor(e).getSelectionEnd());
            getModel(e).setItalic(range);
            refreshCaret(e, range);
        }

        public ItalicAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("insert.character.italic"), editor);
            setIcon(new ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_italic.png")));
            setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        }
    }

    public static class UnderlineAction extends AbstractCharacterAction {
        public void action(ActionEvent e) throws Exception {
            Range range = new Range(getEditor(e).getSelectionStart(), getEditor(e).getSelectionEnd());
            getModel(e).setUnderline(range);
            refreshCaret(e, range);
        }

        public UnderlineAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("insert.character.underline"), editor);
            setIcon(new ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_underline.png")));
            setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.CTRL_MASK));
        }
    }

    public static class CodeAction extends AbstractCharacterAction {
        public void action(ActionEvent e) throws Exception {
            Range range = new Range(getEditor(e).getSelectionStart(), getEditor(e).getSelectionEnd());
            getModel(e).setCode(range);
            refreshCaret(e, range);
        }

        public CodeAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("insert.character.code"), editor);
            setIcon(new ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_codespan.png")));
            setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.ALT_MASK));
        }
    }

    public static class StandardAction extends AbstractCharacterAction {
        public void action(ActionEvent e) throws Exception {
            Range range = new Range(getEditor(e).getSelectionStart(), getEditor(e).getSelectionEnd());
            getModel(e).setStandard(range);
            refreshCaret(e, range);
        }

        public StandardAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("insert.character.standard"), editor);
            setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        }
    }


}
